using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace OgilvyOne.Utility
{
    public class SymmCrypto
    {
        // Methods
        public static string Decrypt(string source)
        {
            PasswordDeriveBytes bytes = new PasswordDeriveBytes("", null);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] rgbKey = bytes.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
            byte[] buffer = Convert.FromBase64String(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Unicode.GetString(stream.ToArray());
        }

        public static string Encrypt(string source)
        {
            PasswordDeriveBytes bytes = new PasswordDeriveBytes("", null);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] rgbKey = bytes.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
            byte[] buffer = Encoding.Unicode.GetBytes(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }

    }
}
